package sis.security;

import static org.junit.Assert.*;

import java.lang.reflect.Method;

import org.junit.Before;
import org.junit.Test;

import sis.studentinfo.Account;
import sis.studentinfo.Permissionexception;

public class SecureProxyTest {
	
	private static final String secureMethodName = "secure";
	private static final String insecureMethodName = "insecure";
	private Account account;
	private SecureProxy proxy;
	private boolean secureMethodCalled;
	private boolean insecureMethodCalled;
	
	@Before
	@SuppressWarnings("unused")
	public void init() {
		account = new Account() {
			public void secure() {
				secureMethodCalled = true;
			}
			public void insecure() {
				insecureMethodCalled = true;
			}
		};
		proxy = new SecureProxy(account, secureMethodName);
	}
	
	@Test
	public void testSecureMethod() throws Throwable {
		Method secureMethod = account.getClass().getDeclaredMethod(secureMethodName, new Class[] {});
		try {
			proxy.invoke(proxy, secureMethod, new Object[] {});
			fail("expected PermissionException");
		} catch (Permissionexception expected) {
			assertFalse(secureMethodCalled);
		}
	}
	
	@Test
	public void testInsecureMethod() throws Throwable {
		Method insecureMethod = account.getClass()
				.getDeclaredMethod(insecureMethodName, new Class[] {});
		proxy.invoke(proxy, insecureMethod, new Object[] {});
		assertTrue(insecureMethodCalled);
	}
	
	
	
	
}
